<?php

class Damages_Methods_Request_DealerCarDamageClaim extends Damages_Methods_Request {

  /**
   *
   * @var int
   */
  public $id;

  public function __construct($params) {
    parent::__construct($params);
    $this->id = (int) $params->id;
  }

  public function process() {
    if (!Damages_Auth::getInstance()->checkPermission(Damages_Auth::PERMISSION_SD))
      throw new Damages_Exception_App(Damages_Codes::ACCESS_DENIED);
    $data = $this->getDbObject();
    if ($data) {
      $this->validate($data);
      $data->setDealerReportDate(time());
      $data->save();
    }
    else
      throw new Damages_Exception_App(Damages_Codes::DB_OBJECT_NOT_FOUND);
    $response = new Damages_Methods_Response(true);
    return $response;
  }

  /**
   * 
   * @return DamagesCarDamages
   */
  private function getDbObject() {
    $data = DamagesCarDamagesQuery::create()->findOneById($this->id);
    return $data;
  }
  
  private function validate(DamagesCarDamages $carDamage){
    if (in_array($carDamage->getDictStatuses()->getCode(), array(DamagesDictStatuses::CODE_NOT_ACCEPTED_CLOSED,DamagesDictStatuses::CODE_NOT_ACCEPTED_OL,DamagesDictStatuses::CODE_NOT_ACCEPTED_SD)))
      throw new Damages_Exception_App(Damages_Codes::ACTION_NOT_ALLOWED_DAMAGE_STATUS);
  }

}